<template>
	<div class="system_menu_wrapper">
		<sw-common-frame class="menubox" :loading="loading">
			<template slot="button">
				<el-button size="small" type="primary" icon="el-icon-plus" @click="handleOpenDialog()">新增</el-button>
			</template>
			<el-form slot="query" size="small" inline>
				<el-form-item>
					<el-input v-model="queryData.name" clearable placeholder="菜单名称" style="width: 200px"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" icon="el-icon-search" @click="handleQueryData()">查询</el-button>
				</el-form-item>
			</el-form>
			<el-table slot="table" :data="tableListData" v-bind="tableConfig">
				<el-table-column prop="name" label="菜单名称" min-width="160" show-overflow-tooltip></el-table-column>
				<el-table-column label="菜单路径/按钮ID" min-width="240">
					<template slot-scope="{ row }">
						<template v-if="row.menuLevel !== '4'">
							<el-tag size="small" type="success">菜单</el-tag>
							{{ row.url }}
						</template>
						<template v-else>
							<el-tag size="small" type="warning">按钮</el-tag>
							{{ row.id }}
						</template>
					</template>
				</el-table-column>
				<el-table-column label="状态" align="center" width="80">
					<template slot-scope="{ row }">
						<el-tag size="small" v-if="row.del === '1'">正常</el-tag>
						<el-tag size="small" v-if="row.del === '2'">禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="158" align="right" header-align="left">
					<template slot-scope="{ row }">
						<template v-if="Number(row.menuLevel) < 3">
							<el-button size="medium" type="text" @click="handleAddChildren(row)">添加子级</el-button>
						</template>
						<el-button size="medium" type="text" @click="handleOpenDialog(row.id)">修改</el-button>
						<el-button size="medium" type="text" @click="handleDeleteData(row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</sw-common-frame>
		<form-dialog v-model="formDialog" :data-id="dataId" :list="tableData" :is-sub.sync="isChildren" @refresh="queryTableData()"></form-dialog>
	</div>
</template>

<script>
import lodash from 'lodash';
import formDialog from './form';
import listMixins from '@/mixins/listMixins';
import { getMenuTree, deleteMenu } from '@/api/system/tbSysMenu';
export default {
	mixins: [listMixins],
	components: { formDialog },
	data() {
		return {
			pageName: '菜单',
			isChildren: false,
			tableConfig: {
				ref: 'table',
				rowKey: 'id',
				height: '100%',
				size: 'medium',
				treeProps: {
					hasChildren: 'hasChildren',
					children: 'childList'
				}
			},
			queryName: ''
		};
	},
	computed: {
		listFunction() {
			return eval(getMenuTree);
		},
		deleteFunction() {
			return eval(deleteMenu);
		},
		requestParams() {
			return { pId: '0' };
		},
		tableListData() {
			let name = this.queryName.toLowerCase();
			return this.$filterTreeByKey(this.tableData, name, 'name', 'childList');
		}
	},
	watch: {
		tableListData(arr) {
			this.$refs['table'].doLayout();
		}
	},
	methods: {
		handleQueryData() {
			this.queryName = lodash.cloneDeep(this.queryData.name);
		},
		//添加子级
		handleAddChildren(data) {
			this.dataId = data.id;
			this.formDialog = true;
			this.isChildren = true;
		},
		handleRowClick(data) {
			if (this.checkedMenu === data.id) return false;
			this.checkedMenu = data.id;
		}
	}
};
</script>
<style lang="scss" scoped>
.system_menu_wrapper {
	display: flex;
	height: 100%;
	.menubox {
		flex: 1;
		overflow: hidden;
	}
}
</style>
